<?php
/**
 * 地铁
 */

namespace app\admin\controller;


use app\admin\model\Metro as MetroModel;
use app\common\controller\AdminController;
use think\Exception;
use think\facade\Request;

class Metro extends AdminController {

    protected $isarrayresult = ['getlist', 'editget'];

    public $resubmitlock = ['editpost'];

    public $levelConfig = [
        1 => ['getlist', 'editget'],
        2 => ['editpost', 'addlinepost', 'addsitepost', 'editsort'],
        4 => [],
        8 => []
    ];

    /**
     * 地铁列表
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $list = MetroModel::getList($params);
            return $this->resData('success', $list);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改站点获取
     * @return false|string
     */
    public function editGet() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $code = MetroModel::getThisName($params, $re);
            if (!$code) {
                return $this->resErr();
            } else {
                return $this->resData('success', ['info' => $re]);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改站点提交
     * @return false|string
     */
    public function editPost() {
        try {
            $params = $this->checkParams('name,id,city_code,lat,lng');
            if ($params === false) return $this->resErr();
            $res = MetroModel::edit($params);
            if (!$res) {
                return $this->resErr();
            } else {
                return $this->resData('编辑成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 新增线路
     * @return false|string
     */
    public function addLinePost() {
        try {
            $params = $this->checkParams('name');
            if ($params === false) return $this->resErr();
            if (!MetroModel::addLinePost($params)) {
                return $this->resErr();
            } else {
                return $this->resData('添加成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 新增站点提交
     */
    public function addSitePost() {
        try {
            $params = $this->checkParams('line_id,type,name,lng,lat');
            if ($params === false) return $this->resErr();
            if (!MetroModel::addSitePost($params)) {
                return $this->resErr();
            } else {
                return $this->resData('添加成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 站点排序
     * @return false|string
     * @throws \Exception
     */
    public function editSort() {
        try {
            $params = $this->checkParams('site_ids');
            if ($params === false) return $this->resErr();
            MetroModel::editSort($params);
            return $this->resData('设置成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}
